/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aaron.platform.service;

import com.aaron.platform.dao.ILxkrProjectDao;
import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.ItscDomainInfo;
import com.aaron.platform.entity.ZdwLxkrProjectInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

/**
 *
 * @author aaronbamboo
 */
public class LxkrProjectMgrImp implements ILxkrProjectMgr {
    
    private ILxkrProjectDao lxkrProjectDao;
    
    private IDomainService domainService;

    public ZdwLxkrProjectInfo findById(Integer lxkrProjectId) {
        return lxkrProjectDao.findById(lxkrProjectId);
    }

    public List<ZdwLxkrProjectInfo> findByName(String ResidenceName) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ZdwLxkrProjectInfo.class);
        detachedCriteria = detachedCriteria.add(Expression.ilike(ZdwLxkrProjectInfo.RESIDENCE_NAME, ResidenceName, MatchMode.ANYWHERE));
        return lxkrProjectDao.findByCriteria(detachedCriteria);
    }

    public PageInfo findByCondition(Properties condition, int startIndex, int pageSize) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ZdwLxkrProjectInfo.class);
        if (condition != null && !condition.isEmpty()) {
           
            // domain id 区域
            Integer domainId = Integer.valueOf(condition.getProperty(ZdwLxkrProjectInfo.DOMAIN_ID));
            if (domainId != null && domainId.intValue() != 0 ) {
                List<Integer> domainIds = new ArrayList<Integer>();
                domainIds.add(domainId);
                List<ItscDomainInfo> domainInfos = domainService.findSubDomains(domainId);
                if (domainInfos != null && domainInfos.size() > 0) {
                    for (ItscDomainInfo domainInfo : domainInfos) {
                        domainIds.add(domainInfo.getDomainId());
                    }
                }
                detachedCriteria = detachedCriteria.add(Expression.in(ZdwLxkrProjectInfo.DOMAIN_ID,
                        domainIds));
            }
            
            // create user 创建者
            String createUserName = condition.getProperty(ZdwLxkrProjectInfo.CREATE_USER_NAME);
            if (createUserName != null && StringUtils.isNotEmpty(createUserName)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(ZdwLxkrProjectInfo.CREATE_USER_NAME,
                        createUserName));
            }
            
            // residence name 小区名
            String residenceName = condition.getProperty(ZdwLxkrProjectInfo.RESIDENCE_NAME);
            if (residenceName != null && StringUtils.isNotEmpty(residenceName)) {
                detachedCriteria = detachedCriteria.add(Expression.ilike(ZdwLxkrProjectInfo.RESIDENCE_NAME,
                        residenceName, MatchMode.ANYWHERE));
            }

            // 施工单位
            int enterprsieId = condition.getProperty(ZdwLxkrProjectInfo.ENTERPRISE_ID) == null ? 0 : Integer.valueOf(condition.getProperty(ZdwLxkrProjectInfo.ENTERPRISE_ID));
            if (enterprsieId != 0) {
                detachedCriteria = detachedCriteria.add(Expression.eq(ZdwLxkrProjectInfo.ENTERPRISE_ID, enterprsieId));
            }
        }

        // 排序
        detachedCriteria.addOrder(Order.asc(ZdwLxkrProjectInfo.LXKR_PROJECT_ID));

        return lxkrProjectDao.findPageByCriteria(detachedCriteria,  startIndex, pageSize);
    }
    
    public List<ZdwLxkrProjectInfo> findByCondition(Properties condition) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ZdwLxkrProjectInfo.class);
        if (condition != null && !condition.isEmpty()) {
           
            // domain id 区域
            Integer domainId = Integer.valueOf(condition.getProperty(ZdwLxkrProjectInfo.DOMAIN_ID));
            if (domainId != null && domainId.intValue() != 0 ) {
                List<Integer> domainIds = new ArrayList<Integer>();
                domainIds.add(domainId);
                List<ItscDomainInfo> domainInfos = domainService.findSubDomains(domainId);
                if (domainInfos != null && domainInfos.size() > 0) {
                    for (ItscDomainInfo domainInfo : domainInfos) {
                        domainIds.add(domainInfo.getDomainId());
                    }
                }
                detachedCriteria = detachedCriteria.add(Expression.in(ZdwLxkrProjectInfo.DOMAIN_ID,
                        domainIds));
            }
            
            // create user 创建者
            String createUserName = condition.getProperty(ZdwLxkrProjectInfo.CREATE_USER_NAME);
            if (createUserName != null && StringUtils.isNotEmpty(createUserName)) {
                detachedCriteria = detachedCriteria.add(Expression.eq(ZdwLxkrProjectInfo.CREATE_USER_NAME,
                        createUserName));
            }
            
            // residence name 小区名
            String residenceName = condition.getProperty(ZdwLxkrProjectInfo.RESIDENCE_NAME);
            if (residenceName != null && StringUtils.isNotEmpty(residenceName)) {
                detachedCriteria = detachedCriteria.add(Expression.ilike(ZdwLxkrProjectInfo.RESIDENCE_NAME,
                        residenceName, MatchMode.ANYWHERE));
            }

            // 施工单位
            int enterprsieId = condition.getProperty(ZdwLxkrProjectInfo.ENTERPRISE_ID) == null ? 0 : Integer.valueOf(condition.getProperty(ZdwLxkrProjectInfo.ENTERPRISE_ID));
            if (enterprsieId != 0) {
                detachedCriteria = detachedCriteria.add(Expression.eq(ZdwLxkrProjectInfo.ENTERPRISE_ID, enterprsieId));
            }
        }

        // 排序
        detachedCriteria.addOrder(Order.asc(ZdwLxkrProjectInfo.LXKR_PROJECT_ID));

        return lxkrProjectDao.findByCriteria(detachedCriteria);
    }

    public void addLxkrProject(ZdwLxkrProjectInfo lxkrProjectInfo) throws LxgxExistsException {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ZdwLxkrProjectInfo.class);
        detachedCriteria = detachedCriteria.add(Expression.eq(ZdwLxkrProjectInfo.RESIDENCE_NAME, lxkrProjectInfo.getResidenceName()));
        List<ZdwLxkrProjectInfo> lxkrProjectInfos = lxkrProjectDao.findByCriteria(detachedCriteria);
        if (lxkrProjectInfos != null && lxkrProjectInfos.size() > 0) {
            throw new LxgxExistsException();
        }
        lxkrProjectDao.save(lxkrProjectInfo);
    }

    public void updateLxkrProject(ZdwLxkrProjectInfo lxkrProjectInfo) {
        lxkrProjectDao.update(lxkrProjectInfo);
    }

    public void removeLxkrProject(Integer lxkrProjectId) {
        lxkrProjectDao.removeById(lxkrProjectId);
    }

    public void removeLxkrProject(ZdwLxkrProjectInfo lxkrProjectInfo) {
        lxkrProjectDao.remove(lxkrProjectInfo);
    }

    public void setLxkrProjectDao(ILxkrProjectDao lxkrProjectDao) {
        this.lxkrProjectDao = lxkrProjectDao;
    }

    public void setDomainService(IDomainService domainService) {
        this.domainService = domainService;
    }
    
    
}
